/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.pathfinder.wizards.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import pt.iasd.youthshelter.datacore.controller.exceptions.NonexistentEntityException;
import pt.iasd.youthshelter.datamodel.PathfinderRole;
import pt.iasd.youthshelter.datamodel.service.PathfinderRoleController;
import pt.iasd.youthshelter.pathfinder.wizards.PathfinderRoleTablePanel;

@ActionID(
        category = "Management",
        id = "pt.iasd.youthshelter.pathfinder.wizards.actions.ShowPathfinderRolesPanelAction")
@ActionRegistration(
        iconBase = "pt/iasd/youthshelter/pathfinder/wizards/actions/icon.png",
        displayName = "#CTL_ShowPathfinderRolesPanelAction")
@ActionReferences({
    @ActionReference(path = "Menu/Management", position = 600),
    @ActionReference(path = "Toolbars/Management", position = 600)
})
@Messages("CTL_ShowPathfinderRolesPanelAction=Assign Role")
public final class ShowPathfinderRolePanelAction implements ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/pathfinder/Bundle");
    
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void actionPerformed(ActionEvent e) {
        ProgressHandle pHandle = ProgressHandleFactory.createHandle(
                    bundle.getString("ProgressBarWizard"));
        pHandle.start();
        final PathfinderRoleTablePanel pathfinderRolePanel = new PathfinderRoleTablePanel();
        DialogDescriptor dialog = new DialogDescriptor(pathfinderRolePanel, bundle.getString("PathfinderRole.Panel"), true, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == DialogDescriptor.OK_OPTION) {
                    Set<Integer> edited = pathfinderRolePanel.getEditedPathfinderRoles();
                    Set<Integer> removed = pathfinderRolePanel.getRemovedPathfinderRoles();
                    PathfinderRoleController controller = Lookup.getDefault().lookup(PathfinderRoleController.class);
                    for(PathfinderRole pathfinderRole : pathfinderRolePanel.getPathfinderRoles()) {
                        if(pathfinderRole.getId() == null) {
                            controller.create(pathfinderRole);
                        } else {
                            int pathfinderRoleId = pathfinderRole.getId();
                            if(removed.contains(pathfinderRoleId)) {
                                try {
                                    controller.destroy(pathfinderRoleId);
                                } catch (NonexistentEntityException ex) {
                                    logger.log(Level.INFO, String.format(
                                        bundle.getString("PathfinderRole.Remove.Error")
                                            .replace("\n", System.getProperty("line.separator")), pathfinderRoleId));
                                }
                            } else if(edited.contains(pathfinderRoleId)) {
                                try {
                                    controller.edit(pathfinderRole);
                                } catch (NonexistentEntityException ex) {
                                    controller.create(pathfinderRole);
                                } catch (Exception ex) {
                                    logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                                    NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                            String.format(bundle.getString("PathfinderRole.Save.Error")
                                                    .replace("\n", System.getProperty("line.separator")),
                                                    ex.getLocalizedMessage()),
                                            NotifyDescriptor.ERROR_MESSAGE);
                                    DialogDisplayer.getDefault().notify(dialog);
                                }
                            }
                        }
                    }
                }
            }
        });
        pHandle.finish();
        DialogDisplayer.getDefault().notify(dialog);
    }
}
